import request from '@/utils/request'

/**
 * 添加资质证明
 */
export function addQualificantion (data) {
  const values = { ...data }
  if (data.attachment && data.attachment.length > 0) {
    const urls = data.attachment.map(item => item.fileUrl)


    values.attachment = urls.join(',')
  } else {
    values.attachment = ''
  }
  // values.delFlag = 1
  values.approvalSt = 0

  console.log('addQualificantion', values);
  return request({
    url: '/basic-api/company/ComQualification/quaAdd',
    method: 'POST',
    data: values
  })
}

/**
 * 编辑资质详情
 */
export function editQualificantion (data) {
  const values = { ...data }
  if (data.attachment && data.attachment.length > 0) {
    const urls = data.attachment.map(item => item.fileUrl)
    values.attachment = urls.join(',')
  }
  console.log('编辑资质详情', values);
  return request({
    url: '/basic-api/company/ComQualification/quaEdit',
    method: 'put',
    data: values
  })
}


/**
 * 获取资质证明列表
 */
export async function getQualificantionList (data) {

  const res = await request({
    url: '/basic-api/company/ComQualification/quaList',
    method: 'get',
    params: data
  })
  if (res.code === 200) {
    res.rows.forEach(item => {
      if (item.attachment && item.attachment.length > 0) {
        item.attachment = item.attachment.split(',')
      }
    })
  }

  console.log('getQualificantionList', res);
  return res
}

/**
 * 企业前端-获取资质证明列表
 * @param {*} data 
 * @returns 
 */
export async function getClientUseQualificantionList (data) {

  const res = await request({
    url: '/basic-api/company/noAuth/ComQualificationList',
    method: 'get',
    params: data
  })
  if (res.code === 200) {
    res.rows.forEach(item => {
      if (item.attachment && item.attachment.length > 0) {
        item.attachment = item.attachment.split(',')
      }
    })
  }

  console.log('getQualificantionList', res);
  return res
}


/**
 * 获取资质详情
 */
export async function getQualificantionDetail (id) {
  const res = await request({
    url: '/basic-api/company/ComQualification/quaGetInfo/' + id,
    method: 'get',
  })
  if (res.code === 200) {
    if (res.data.attachment && res.data.attachment.length > 0) {
      res.data.attachment = res.data.attachment.split(',').map(fileUrl => {
        return {
          fileUrl
        }
      })
    }
  }
  console.log('获取资质详情', res);
  return res
}

/**
 * 删除资质证明
 */
export function delQualificantion (id) {
  return request({
    url: '/basic-api/company/ComQualification/quaRemove/' + id,
    method: 'delete',
  })

}


